import ImgPreviewComponent from './ImgPreview'

const ImgPreview = {}

ImgPreview.install = Vue => {
  const ImgConstructor = Vue.extend(ImgPreviewComponent)

  const instance = new ImgConstructor()

  instance.$mount(document.createElement('div'))

  document.body.appendChild(instance.$el)

  Vue.prototype.$imgPreview = (e) => {
    instance.target = e.currentTarget
    let param =instance.target?(instance.target.getAttribute('data-imgpreview') || [topVue.GLOBAL.defaultImg]):e.url
    //拓展参数，假如是list，默认展示第几张图片
    if(param.indexOf('data:image/png;base64')>-1){
    // base64
  		instance.imgs = [param]
    }else if(typeof(param)=='string'){
  		instance.imgs = param.split(',').map(url => {
        return {
          src: url,
          dataType: !instance.target||instance.target.getAttribute('data-type') === null
            ? 'mp4,3gp,mpeg,mavip4,mov,flv'.includes(url.split('.')[url.split('.').length - 1].toLowerCase()) ? true : false
            : instance.target.getAttribute('data-type').indexOf('video')>-1
        }
      })
  	}else if(Array.isArray(param)){
  		instance.imgs = param
  	}
  	instance.imgIndex =instance.target?(parseInt(instance.target.getAttribute('data-imgpreviewindex')) || 0 ):0//默认展示第一张
    instance.transform = {   // 设置初始缩放
      scale: 1,
      deg: 0,
      offsetX: 0,
      offsetY: 0,
      enableTransition: false,
    }
  	instance.isShowImgViewer = true
  }
}

export default ImgPreview
